package com.yuanxin.chooseimage;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.util.ArrayList;
import java.util.List;

public class PermissionActivity extends AppCompatActivity {
    private static final String TAG = "PermissionActivity";
    private static final int REQUEST_CODE = 0x001;//请求码
    private String[] needPersition = new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE, Manifest.permission.CALL_PHONE};
    private List<String> denyPermissions = new ArrayList<>();//记录没有通过的权限

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // Log.e(TAG, "申请权限");
        findDeniedPermissions(PermissionActivity.this, needPersition);
        if (denyPermissions.size() > 0) {
            //弹出申请权限提示框
            ActivityCompat.requestPermissions(PermissionActivity.this,
                    denyPermissions.toArray(new String[denyPermissions.size()]),//将集合转换为数组格式
                    REQUEST_CODE);
           // Log.e(TAG, "弹出申请权限提示框");
        } else {
           // Log.e(TAG, "没有拒绝的授权");
            setResult(0);
            finish();
        }
    }


    @TargetApi(Build.VERSION_CODES.M)
    public List<String> findDeniedPermissions(Activity activity, String... permission) {
        //遍历所需的权限
        for (String value : permission) {
            //检查权限是否通过，如果没有通过添加到集合中
            if (ActivityCompat.checkSelfPermission(this,value) != PackageManager.PERMISSION_GRANTED) {
                denyPermissions.add(value);
               // Log.e(TAG, "用户拒绝了" + value);
            }
        }
        return denyPermissions;
    }



    //申请权限回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CODE) {
            if (grantResults.length == permissions.length) {
                denyPermissions.clear();//清除集合
                for (int i = 0; i < grantResults.length; i++) {
                    //如果用户拒绝了权限申请，添加到集合中
                    if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                        denyPermissions.add(permissions[i]);
                       // Log.e(TAG, "用户第一次申请拒绝了" + permissions[i]);
                    }
                }
                //如果集合中有数据则表示有权限申请未通过
                if (denyPermissions.size() > 0) {
                    //不再进行二次请求，
                    Toast.makeText(this, "需要授予相关权限，某些功能才能正常使用。", Toast.LENGTH_LONG).show();
                    finish();
                } else {
                    //没有未通过的权限
                   // Log.e(TAG, "用户通过了所有授权");
                    finish();
                }
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
